#include "pool.h"

void *count_time(void *arg)//读秒
{
    int i = 1;
    while(1)
    {
        fprintf(stderr, "%d\n",i++);
        sleep(1);        
    }
}

void *mytask(void *arg)//任务
{
    int a = (int)arg;
    printf("[%u]在执行任务,预计花费%d秒\n",pthread_self(), a);
    sleep(a);
    printf("[%u]完成任务,花费%d秒\n",pthread_self(), a);

    return NULL;
}

int main(int argc, char const *argv[])
{
    //读秒
    pthread_t c;
    pthread_create(&c, NULL, count_time, NULL);

    //初始化线程池
    thread_pool *pool = malloc(sizeof(thread_pool));
    init_pool(pool, 2);

    //添加若干任务
    srand(time(NULL));//随机时间
    printf("添加3个任务\n");
    add_task(pool, mytask, (void *)(rand()%7+1));
    add_task(pool, mytask, (void *)(rand()%7+1));
    add_task(pool, mytask, (void *)(rand()%7+1));

    //等待任务执行
    printf("活跃线程数[%d]\n",pool->active_threads);
    sleep(12);


    return 0;
}
